package pl.pw.gis.gc_sa.tests;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.junit.BeforeClass;
import org.junit.Test;

import pl.pw.gis.gc_sa.algo.GcSolver;
import pl.pw.gis.gc_sa.algo.RandomSearch;
import pl.pw.gis.gc_sa.algo.SimulatedAnnealing;
import pl.pw.gis.gc_sa.data.Graph;
import pl.pw.gis.gc_sa.utils.Utils;

/**
 * Klasa zawierajaca testy wykonywane przy pomocy biblioteki jUnit.
 * 
 * W tej klasie testowane sa:
 * - skutecznosc dzialania trzech algorytmow kolorowania grafu (ze sprawozdania: RS, SA1, SA2)
 * - szybkosc dzialania trzech powyzszych algorytmow
 * 
 * @author Lukasz
 * 
 */
public class AlgorithmsComparisonTest {
    private static final long MILLIS_LIMIT_PER_RUN = 300000;
    private static final int NO_SOLVERS = 3;

    static Vector<Graph> mGraphs;
    static Vector<GcSolver> mSolvers;

    @BeforeClass
    public static void prepare() throws Exception {
        mGraphs = new Vector<Graph>();

        mGraphs.add(Graph.createVPGraph(10, 0.5f));
        mGraphs.add(Graph.createVPGraph(50, 0.5f));
        mGraphs.add(Graph.createVPGraph(100, 0.5f));
        mGraphs.add(Graph.createVPGraph(500, 0.5f));
        mGraphs.add(Graph.createVPGraph(1000, 0.5f));
    }

    @Test
    public void effectivenessTest() throws InterruptedException {
        GcSolver[] solvers = new GcSolver[NO_SOLVERS];
        for (Graph graph : mGraphs) {
            solvers[0] = new RandomSearch(graph);
            solvers[1] = new SimulatedAnnealing(graph);
            solvers[2] = new SimulatedAnnealing(graph, true, false, true,
                    SimulatedAnnealing.COST_COLOR_COUNT);

            Utils.log("Testing " + graph.getGraphInfo()
                    + "\n----------------------");

            for (int i = 0; i < NO_SOLVERS; i++) {
                solvers[i].start();
                solvers[i].join(MILLIS_LIMIT_PER_RUN);

                Utils.log(solvers[i].getMethodName() + ": "
                        + solvers[i].getMethodOutcome());

            }
        }
    }

    @Test
    public void MycielskiTest() throws Exception {
        final String MYCIELSKI_DIR = "D:/workspace/SimulatedAnnealing/src/pl/pw/gis/gc_sa/data/Mycielski/";
        for (int i = 3; i <= 8; i++) {
            for(int j=0; j<10; j++) {
                String fullPath = MYCIELSKI_DIR + "Mycielski_" + String.valueOf(i) + "_mx.txt";
                Graph graph = Graph.createGraphFromFile(fullPath);
                GcSolver sa = new SimulatedAnnealing(graph);
                sa.start();
                sa.join(MILLIS_LIMIT_PER_RUN);
                Utils.log("Mycielski" + String.valueOf(i) + ": " + sa.getMethodOutcome());
            }
        }
    }

    @Test
    public void speedTest() throws Exception {
        List<Graph> graphs = new ArrayList<Graph>();
        graphs.add(Graph.createVPGraph(1000, 0.01f));
        graphs.add(Graph.createVPGraph(1000, 0.2f));
        graphs.add(Graph.createVPGraph(1000, 0.5f));
        graphs.add(Graph.createVPGraph(1000, 1.0f));

        GcSolver[] solvers = new GcSolver[4];
        for (Graph graph : graphs) {
            solvers[0] = new RandomSearch(graph);
            solvers[1] = new SimulatedAnnealing(graph);
            solvers[2] = new SimulatedAnnealing(graph, true, false, true,
                    SimulatedAnnealing.COST_COLOR_COUNT);

            Utils.log("Testing " + graph.getGraphInfo()
                    + "\n----------------------");

            for (int i = 0; i < NO_SOLVERS; i++) {
                solvers[i].start();
                solvers[i].join(MILLIS_LIMIT_PER_RUN);

                Utils.log(solvers[i].getMethodName() + ": "
                        + solvers[i].getComputingTime() + "ms");

            }
        }
    }
}
